package m.jclecai.com.adapter;

import android.content.Context;
import android.graphics.Color;
import android.text.Html;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import m.jclecai.com.R;

import java.text.DecimalFormat;
import java.util.List;

import m.jclecai.com.bean.SFCItme;
import m.jclecai.com.listener.NotifyTextViewSetChanged;
import m.jclecai.com.utils.Utils;

/**
 * Created by hp on 2017/4/6.
 */

public class ExpandableRJCAdapter extends BaseAdapter {
    private Context context;
    private List<SFCItme> list;
    private NotifyTextViewSetChanged changed;


    public ExpandableRJCAdapter() {
    }

    public ExpandableRJCAdapter(Context context, List<SFCItme> list,NotifyTextViewSetChanged changed) {
        this.context = context;
        this.list = list;
        this.changed=changed;
    }

    @Override
    public int getCount() {
        return list==null?0:list.size();
    }

    @Override
    public Object getItem(int position) {
        return list.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
      ChildViewHolder childViewHolder;
        if (convertView == null) {
            //  convertView = View.inflate(context, R.layout.activity_lottery_sfc_item, null);//根布局的最外层的宽高是没有效果的，用这种方式加载布局出现问题
            convertView= LayoutInflater.from(context).inflate(R.layout.activity_lottery_sfc_item,parent,false);
            childViewHolder = new ChildViewHolder(convertView);
            convertView.setTag(childViewHolder);
        } else {
            childViewHolder = (ChildViewHolder) convertView.getTag();
        }
        //名字
        childViewHolder.item_name.setText(Html.fromHtml("<font color='#818181' >"
                + list.get(position).getBout_index() + " </font><font color='"
                + list.get(position).getColor() + "' >" + list.get(position).getMatch_name() + "</font>"));
        //时间
        if (!TextUtils.isEmpty(list.get(position).getMatch_time())) {
            childViewHolder.item_time.setText(list.get(position).getMatch_time().substring(11, 16));
        }
        //客队
        childViewHolder.away_team_name.setText(list.get(position).getAway_team());
        //主队
        childViewHolder.home_team_name.setText(list.get(position).getHome_team());

        DecimalFormat df = new DecimalFormat("#.00");

        if (TextUtils.isEmpty(list.get(position).getOdd_home())) {
            childViewHolder.odd_home.setText("0.00");
        } else {
            childViewHolder.odd_home.setText(df.format(Double.parseDouble(list.get(position).getOdd_home())));
        }
        if (TextUtils.isEmpty(list.get(position).getOdd_draw())) {
            childViewHolder.odd_draw.setText("0.00");
        } else {
            childViewHolder.odd_draw.setText(df.format(Double.parseDouble(list.get(position).getOdd_draw())));
        }
        if (TextUtils.isEmpty(list.get(position).getOdd_away())) {
            childViewHolder.odd_away.setText("0.00");
        } else {
            childViewHolder.odd_away.setText(df.format(Double.parseDouble(list.get(position).getOdd_away())));
        }
        //左边第一个
        if (list.get(position).isOdd_home_select()) {
            childViewHolder.odd_home.setBackgroundColor(android.graphics.Color
                    .parseColor("#FF0026"));//将背景设为红色
            childViewHolder.odd_home.setTextColor(Color.WHITE);//字体设为白色
        } else {
            childViewHolder.odd_home.setBackgroundColor(Color.WHITE);
            childViewHolder.odd_home.setTextColor(Color.BLACK);
        }
        //中间一个
        if (list.get(position).isOdd_draw_select()) {
            childViewHolder.odd_draw.setBackgroundColor(android.graphics.Color
                    .parseColor("#FF0026"));
            childViewHolder.odd_draw.setTextColor(Color.WHITE);
        } else {
            childViewHolder.odd_draw.setBackgroundColor(Color.WHITE);
            childViewHolder.odd_draw.setTextColor(Color.BLACK);
        }
        //右边一个
        if (list.get(position).isOdd_away_select()) {
            childViewHolder.odd_away.setBackgroundColor(android.graphics.Color
                    .parseColor("#FF0026"));
            childViewHolder.odd_away.setTextColor(Color.WHITE);
        } else {
            childViewHolder.odd_away.setBackgroundColor(Color.WHITE);
            childViewHolder.odd_away.setTextColor(Color.BLACK);
        }

        childViewHolder.odd_home.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (list.get(position).isOdd_home_select()) {
                    list.get(position).setOdd_select_number(list.get(position).getOdd_select_number() - 1);
                    list.get(position).setOdd_home_select(false);
                } else {
                    list.get(position).setOdd_select_number(list.get(position).getOdd_select_number() + 1);
                    list.get(position).setOdd_home_select(true);
                }
                itemStateChange(list.get(position));
            }
        });


        childViewHolder.odd_draw.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (list.get(position).isOdd_draw_select()) {
                    list.get(position).setOdd_select_number(list.get(position).getOdd_select_number() - 1);
                    list.get(position).setOdd_draw_select(false);
                } else {
                    list.get(position).setOdd_select_number(list.get(position).getOdd_select_number() + 1);
                    list.get(position).setOdd_draw_select(true);
                }
                itemStateChange(list.get(position));
            }
        });
        childViewHolder.odd_away.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (list.get(position).isOdd_away_select()) {
                    list.get(position).setOdd_select_number(list.get(position).getOdd_select_number() - 1);
                    list.get(position).setOdd_away_select(false);
                } else {
                    list.get(position).setOdd_select_number(list.get(position).getOdd_select_number() + 1);
                    list.get(position).setOdd_away_select(true);
                }
                itemStateChange(list.get(position));
            }
        });
        return convertView;
    }

    /**
     * @category 判断item的添加状态
     */
    private void itemStateChange(SFCItme item) {
        // TODO Auto-generated method stub
            if (item.isOdd_home_select() || item.isOdd_draw_select()
                    || item.isOdd_away_select()) {
                if (!Utils.RJC_ITEM_LIST.contains(item)) {
                    Utils.RJC_ITEM_LIST.add(item);
                }
            } else {
                if (Utils.RJC_ITEM_LIST.contains(item)) {
                    Utils.RJC_ITEM_LIST.remove(item);
                }
            }
        // setNumber();
        notifyDataSetChanged();
        changed.onClickSetChangedText();
    }

    static class ChildViewHolder {

        TextView item_name;
        TextView item_time;
        TextView home_team_name;
        TextView away_team_name;
        TextView odd_home;
        TextView odd_away;
        TextView odd_draw;

        public ChildViewHolder(View view) {
            item_name = (TextView) view.findViewById(R.id.item_name);
            item_time = (TextView) view.findViewById(R.id.item_time);
            home_team_name = (TextView) view.findViewById(R.id.home_team_name);
            away_team_name = (TextView) view.findViewById(R.id.away_team_name);
            odd_home = (TextView) view.findViewById(R.id.odd_home);
            odd_away = (TextView) view.findViewById(R.id.odd_away);
            odd_draw = (TextView) view.findViewById(R.id.odd_draw);
        }
    }
}
